﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using ProxyHero.Common;
using LoaMen.Controls;

namespace ProxyHero.Setting
{
    public partial class frmProxySetting : Form
    {
        AutoChangeHelper ah = new AutoChangeHelper();
        public frmProxySetting()
        {
            InitializeComponent();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.chbEnable.Checked)
            {
                string proxy = this.txtProxy.Text.Trim();
                Regex regex = new Regex(@"(?<IP>(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9]))\:(?<Port>\d{1,5})");
                MatchCollection matchs = regex.Matches(proxy);
                if (matchs.Count == 1)
                {
                    ah.SetBrowserProxy(proxy);
                }
            }
            else
            {
                ah.SetBrowserProxy("");
            }
            this.Close();
        }

        private void chbEnable_CheckedChanged(object sender, EventArgs e)
        {
            this.txtProxy.Enabled = this.chbEnable.Checked;
        }

        private void frmProxySetting_Load(object sender, EventArgs e)
        {
            foreach (WebBrowserEx wbe in Config.WebBrowserArr)
            {
                if (!string.IsNullOrEmpty(wbe.ProxyServer))
                {
                    this.txtProxy.Text = wbe.ProxyServer;
                    this.chbEnable.Checked = true;
                    break;
                }
            }
        }
    }
}
